package com.hspedu.jdbc;

import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/4/7
 * @time 17:16
 * 获取数据库连接的五种方式
 */
public class JDBCConnect {
    @Test
    public void connect01() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/classtest";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "GUQIhao123.");
        Connection connect = driver.connect(url, properties);
        System.out.println("connect = " + connect);
    }

    @Test
    public void connect02() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        Class cls = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) cls.newInstance();
        String url = "jdbc:mysql://localhost:3306/classtest";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "GUQIhao123.");
        Connection connect = driver.connect(url, properties);
        System.out.println("connect = " + connect);
    }

    @Test
    public void connect03() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
        Class<?> cls = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) cls.newInstance();
        String url = "jdbc:mysql://localhost:3306/classtest";
        String user = "root";
        String password = "GUQIhao123.";
        //注册driver驱动
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
    }

    @Test
    public void connect04() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {
        Class<?> cls = Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/classtest";
        String user = "root";
        String password = "GUQIhao123.";

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
    }

    @Test
    public void connect05() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException, IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
        connection.close();
    }
}
